package com.seedwill.common.dozer;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.util.MappingUtils;
import org.dozer.util.ReflectionUtils;


public class ABaseConverterSupport {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	private static final Class<?>[] emptyArglist = new Class[0];
	private XmlBeansImplementsConfig config;
	
	
	public XmlBeansImplementsConfig getConfig() {
		return config;
	}


	public void setConfig(XmlBeansImplementsConfig config) {
		this.config = config;
	}
	
//	public void changeType() {
//		XmlString.type;
//	}
	
	public boolean isXmlShortClz(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlShort);
			if (destinationClass.equals(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isXmlShortClz exception", e);
			}
		}
		return false;
	}
	public boolean isXmlDoubleClz(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDouble);
			if (destinationClass.equals(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isXmlDoubleClz exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlObject(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlObject);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlObject exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlLong(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlLong);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlLong exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlDouble(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDouble);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlDouble exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlDecimal(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDecimal);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlDecimal exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlFloat(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlFloat);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlFloat exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlInt(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInt);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlInt exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlInteger(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInteger);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlInteger exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlShort(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlShort);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlShort exception", e);
			}
		}
		return false;
	}
	public boolean isInstanceofXmlString(Object sourceFieldValue) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlString);
			if(sourceFieldValue.getClass().isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isInstanceofXmlString exception", e);
			}
		}
		return false;
	}
	
	public boolean isAssignableFromXmlObject(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlObject);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlObject exception", e);
			}
		}
		return false;
	}
	public boolean isAssignableFromXmlShort(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlShort);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlShort exception", e);
			}
		}
		return false;
	}
	public boolean isAssignableFromXmlDouble(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDouble);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlDouble exception", e);
			}
		}
		return false;
	}	
	public boolean isAssignableFromXmlFloat(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlFloat);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlFloat exception", e);
			}
		}
		return false;
	}		
	public boolean isAssignableFromXmlInt(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInt);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlInt exception", e);
			}
		}
		return false;
	}	
	public boolean isAssignableFromXmlLong(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlLong);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlLong exception", e);
			}
		}
		return false;
	}	
	
	public boolean isAssignableFromXmlDecimal(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDecimal);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlDecimal exception", e);
			}
		}
		return false;
	}	
	public boolean isAssignableFromXmlInteger(Class<?> destinationClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInteger);
			if(destinationClass.isAssignableFrom(destClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isAssignableFromXmlInteger exception", e);
			}
		}
		return false;
	}	
	public boolean isXmlObjectAssignableFrom(Class sourceClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlObject);
			if(destClass.isAssignableFrom(sourceClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isXmlObjectAssignableFrom exception", e);
			}
		}
		return false;
	}
	public boolean isXmlStringAssignableFrom(Class sourceClass) {
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlString);
			if(destClass.isAssignableFrom(sourceClass)) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isXmlObjectAssignableFrom exception", e);
			}
		}
		return false;
	}
	public boolean isStringEnumAbstractBase(Object sourceFieldValue) {
		if(sourceFieldValue.getClass().getName().equals(config.StringEnumAbstractBase)) {
			return true;
		}
		return false;
	}	
	public boolean isJavaStringEnumerationHolderEx(Object sourceFieldValue) {
		if(sourceFieldValue.getClass().getName().equals(config.JavaStringEnumerationHolderEx)) {
			return true;
		}
		return false;
	}
	
	public boolean isXmlDouble(Object sourceFieldValue) {
		if(sourceFieldValue.getClass().getName().equals(config.XmlDouble)) {
			return true;
		}
		return false;
	}
	public boolean isXmlLong(Object sourceFieldValue) {
		if(sourceFieldValue.getClass().getName().equals(config.XmlLong)) {
			return true;
		}
		return false;
	}
	
	public Object newXmlString() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlString);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}	
	
	public Object newXmlShort() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlShort);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}	
	public Object newXmlDouble() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDouble);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	public Object newXmlFloat() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlFloat);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}	
	public Object newXmlDecimal() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlDecimal);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}	
	
	public Object newXmlInteger() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInteger);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}	
	
	public Object newXmlInt() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlInt);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}		
	public Object newXmlLong() throws Exception  {
		Object result = null;
		try {
			Class<?> destClass = org.springframework.util.ClassUtils.forName(config.XmlLong);
			 Class<?>[] innerClasses = destClass.getClasses();
			Class<?> factory = null;
			for (Class<?> innerClass : innerClasses) {
			  if (innerClass.getName().endsWith("Factory")) {
			    factory = innerClass;
			    break;
			  }
			}
			Method newInstanceMethod = null;
			try {
			  newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist);
			} catch (NoSuchMethodException e) {
			  MappingUtils.throwMappingException(e);
			}
			result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	public static Object doMethodGetInvokerResult(Object obj, String mtd) throws Exception {
		try {
			return org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
	public static boolean doMethodInvokerResultBoolean(Object obj, String mtd) throws Exception {
		try {
			return (Boolean)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
	public static String doMethodInvokerResultString(Object obj, String mtd) throws Exception {
		try {
			return (String)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
	public static Long doMethodInvokerResultLong(Object obj, String mtd) throws Exception {
		try {
			return (Long)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void doMethodSetInvokerResult(Object obj, String mtd, Object... args) throws Exception {
		try {
			org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, args);
		} catch (Exception e) {
			throw e;
		}
	}
	
   public boolean isEmpty(String str){
		return str == null || str.trim().length() == 0;
   }
   
   public boolean isEmptyXmlObjectString(Object sourceFieldValue) {
		try {
			if(isEmpty(doMethodInvokerResultString(sourceFieldValue, "getStringValue"))) {
				return true;
			}
		} catch (Exception e) {
			if(e instanceof RuntimeException) {
				throw (RuntimeException)e;
			}else {
				throw new RuntimeException("isEmptyXmlObjectString exception", e);
			}
		}
		return false;
	}
   
   
   
	public static Double doMethodInvokerResultDouble(Object obj, String mtd) throws Exception {
		try {
			return (Double)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
   
	public static BigDecimal doMethodInvokerResultBigDecimal(Object obj, String mtd) throws Exception {
		try {
			return (BigDecimal)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
   
	public static Float doMethodInvokerResultFloat(Object obj, String mtd) throws Exception {
		try {
			return (Float)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
   
	   
	public static Integer doMethodInvokerResultInt(Object obj, String mtd) throws Exception {
		try {
			return (Integer)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
   
	public static BigInteger doMethodInvokerResultBigInteger(Object obj, String mtd) throws Exception {
		try {
			return (BigInteger)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
   
 	public static Short doMethodInvokerResultShort(Object obj, String mtd) throws Exception {
		try {
			return (Short)org.dozer.util.ReflectionUtils.getMethod(obj, mtd).invoke(obj, null);
		} catch (Exception e) {
			throw e;
		}
	}
     
   
   public Long longValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultLong(sourceFieldValue, "getLongValue");
   }
   
   public Double doubleValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultDouble(sourceFieldValue, "getDoubleValue");
   }
   
   public BigDecimal bigDecimalValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultBigDecimal(sourceFieldValue, "getBigDecimalValue");
   }
   
   public Float floatValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultFloat(sourceFieldValue, "getFloatValue");
   }
   
   
   public Integer intValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultInt(sourceFieldValue, "getIntValue");
   }
   
   public BigInteger bigIntegerValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultBigInteger(sourceFieldValue, "getBigIntegerValue");
   }
   
   
   public Short shortValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultShort(sourceFieldValue, "getShortValue");
   }
   
   public String stringValueByXmlObj(Object sourceFieldValue) throws Exception  {
	   return doMethodInvokerResultString(sourceFieldValue, "getStringValue");
   }
	
}
